using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Deepcode.Flux.Core.Data
{
	public class SQLUtility
	{
		#region Parameter creation
		/// <summary>
		/// Generates a bi-directional parameter to a query
		/// </summary>
		/// <param name="FieldName"></param>
		/// <param name="type"></param>
		/// <param name="len"></param>
		/// <param name="val"></param>
		/// <returns></returns>
		public static SqlParameter ParameterInOut(string FieldName, SqlDbType type, int len, object val)
		{
			SqlParameter newParam = new SqlParameter(FieldName, type, len);
			if ((type == SqlDbType.NText) || (type == SqlDbType.Text)) newParam = new SqlParameter(FieldName, type);
			else newParam = new SqlParameter(FieldName, type, len);

			newParam.Direction = ParameterDirection.InputOutput;
			newParam.Value = val;
			return newParam;
		}
		/// <summary>
		/// Generates an Input parameter to a query.
		/// </summary>
		/// <param name="FieldName"></param>
		/// <param name="type"></param>
		/// <param name="len"></param>
		/// <param name="val"></param>
		/// <returns></returns>
		public static SqlParameter ParameterIn(string FieldName, SqlDbType type, int len, object val)
		{
			SqlParameter newParam = null;
			if ((type == SqlDbType.NText) || (type == SqlDbType.Text)) newParam = new SqlParameter(FieldName, type);
			else newParam = new SqlParameter(FieldName, type, len);

			newParam.Value = val;
			return newParam;
		}
		/// <summary>
		/// Generates an Output parameter to a query
		/// </summary>
		/// <param name="FieldName"></param>
		/// <param name="type"></param>
		/// <param name="len"></param>
		/// <returns></returns>
		public static SqlParameter ParameterOut(string FieldName, SqlDbType type, int len)
		{
			SqlParameter newParam = new SqlParameter(FieldName, type, len);
			newParam.Direction = ParameterDirection.Output;
			return newParam;
		}
		#endregion
		#region DataRow field null checkers
		/// <summary>
		/// Checks for null values in a database field
		/// </summary>
		/// <param name="field"></param>
		/// <param name="def"></param>
		/// <returns></returns>
		public static string DBNull(object field, string def)
		{
			if (field == System.DBNull.Value) return def;
			return (string)field;
		}
		/// <summary>
		/// Checks for null values in a database field
		/// </summary>
		/// <param name="field"></param>
		/// <param name="def"></param>
		/// <returns></returns>
		public static int DBNull(object field, int def)
		{
			if (field == System.DBNull.Value) return def;
			if (field is Int16)
			{
				Int16 result = (Int16)field;
				return Convert.ToInt32(result);
			}
			return (int)field;
		}
		/// <summary>
		/// Checks for null values in a database field
		/// </summary>
		/// <param name="field"></param>
		/// <param name="def"></param>
		/// <returns></returns>
		public static DateTime DBNull(object field, DateTime def)
		{
			if (field == System.DBNull.Value) return def;
			return (DateTime)field;
		}
		/// <summary>
		/// Checks for null values in a database field
		/// </summary>
		/// <param name="field"></param>
		/// <param name="def"></param>
		/// <returns></returns>
		public static bool DBNull(object field, bool def)
		{
			if (field == System.DBNull.Value) return def;
			return (bool)field;
		}
		#endregion
	}
}
